package com.aaa.phone.service.registerservice.impl;

import com.aaa.phone.resultcommon.Result;
import com.aaa.phone.dao.registerdao.IRegisterDao;
import com.aaa.phone.service.registerservice.IRegisterService;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.web.shiro.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @program: batterysharing
 * @description: 用户注册
 * @author: 王璐
 * @create: 2018-12-15 16:34
 **/
@Service
public class RegisterServiceImpl implements IRegisterService,ISystemConst {

    @Autowired
    private IRegisterDao dao;

    /**
     * @Description: 用户注册
     * @Author: 王璐
     * @Date: 2018/12/15
     */
    @Override
    public int userRegister(String tel,String psw) {

        int exec = dao.selectTel(tel);
        Result result = new Result();
        String salt = Math.abs(new Random().nextInt())+"";
        //判断用户是否已经注册，如果没有注册,进行用户注册
        if (exec <= 0) {
            //加密后的密码
            String password = ShiroUtil.getPassword(salt,psw);
            //将信息插入到UserLogin表
            Map<Object, Object> map = new HashMap<>();
            map.put("tel",tel);
            map.put("password",password);
            map.put("salt",salt);
            int i = dao.addUser(map);
            if (i > 0) {
                //注册成功，返回成功码100
                result.setCode(SUCCESS_CODE);
                dao.addUserInfo(tel);
                dao.addUserMoney(tel);
            } else {
                //注册失败，返回失败码200
                result.setCode(DEFEAT_CODE);
            }
        }else{
            //用户已注册  返回用户已注册码201
            result.setCode(USER_EXISTED);
        }
        return result.getCode();
    }
}
